
var mongoHelper = function()
{
	this.databaseUrl = "NodeCMS"; // "username:password@example.com/mydb"
	this.collections = ["modules"]
	this.mongo = require("mongojs");
	var DataResponse = function(db) 
	{
		if(db == null)
			return;
			
		this.db = db;
		this.data = null;
		this.err = null;
		this.status = null;
		this.onSuccess = function(data, status)
		{
			console.log("Default Success");
			if(data != null)
				console.log(this.data);
			else if(status != null)
				console.log(status)
			else
				console.log("Success");
		}
		this.onFail = function()
		{
			console.log("Default Fail");
			if(this.err != null)
				console.log(this.err);
			else
				console.log("There was an Error");
		}
		this.ProcessResponse = function(db)
		{
			if( this.err  ) 
				this.onFail();
			else 
			{
				if(typeof this.onSuccess == "function")
				{
					this.onSuccess(this.data, this.status);
				}
			}		
			this.db.close();
		}
	}


	this.Get = function(tmp, query, callback)
	{
		console.log("make connection");
		if(db == null)
			var db = this.mongo.connect(this.databaseUrl, this.collections);	

		db.modules.findOne
		(
			query, 
			function(err, status) 
			{
				
				if( err ) 
				{
					console.log("Error");
					console.log(err);
				}
				else if( !status )
				{
					console.log("No records found");
					for(var propertyName in query) 
					{
						tmp[propertyName] = query[propertyName];
					}
				}
				else 
				{
					console.log("Found One");
					for(var propertyName in status) 
					{
						if(typeof tmp[propertyName] !== "undefined" && tmp[propertyName].length < 1)
							tmp[propertyName] = status[propertyName];
					}
				}
				

				var resp = new DataResponse(db);
				resp.data = tmp;
				resp.err = err;
				resp.status = status;
				if(typeof callback === "function")
					resp.onSuccess = function(d, s){ console.log("calling custom callback");callback(d, s); };
				resp.ProcessResponse();

			}
		);
	}
	
	this.Save = function(data, callback)
	{
		console.log("make connection");
		if(db == null)
			var db = this.mongo.connect(this.databaseUrl, this.collections);	

		db.modules.save
		(
			data,
			function(err, status) 
			{
				console.log("Saved");
				var resp = new DataResponse(db);
				resp.data = data;
				resp.err = err;
				resp.status = status;
				if(typeof callback === "function")
					resp.onSuccess = function(d, s){ console.log("calling custom callback");callback(d, s); };
				resp.ProcessResponse();
			}
		);
	}
	this.Update = function(data, callback)
	{
		console.log("make connection");
		if(db == null)
			var db = this.mongo.connect(this.databaseUrl, this.collections);	

		db.modules.update
		(
			{ _id: data._id },
			data,
			{ multi: false },
			function(err, status) 
			{
				console.log("Updated");
				var resp = new DataResponse(db);
				resp.data = data;
				resp.err = err;
				resp.status = status;
				if(typeof callback === "function")
					resp.onSuccess = function(d, s){ console.log("calling custom callback");callback(d, s); };
				resp.ProcessResponse();
			}
		);
	}
	
	this.Delete = function(data, callback)
	{
		console.log("make connection");
		var db = this.mongo.connect(this.databaseUrl, this.collections);

		db.modules.remove
		(
				data,
				function(err, status) 
				{
					console.log("Deleted");
					var resp = new DataResponse(db);
					resp.data = data;
					resp.err = err;
					resp.status = status;
					if(typeof callback === "function")
						resp.onSuccess = function(d, s){ console.log("calling custom callback");callback(d, s); };
					resp.ProcessResponse();
				}
		);
	}
}

exports.mongoHelper = mongoHelper;